/*
 * Copyright (c) 2009-2020 Arm Limited. All rights reserved.
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Licensed under the Apache License, Version 2.0 (the License); you may
 * not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an AS IS BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*============================ INCLUDES ======================================*/

#include <stdio.h>
#include "app_platform.h"
#include "arm_2d_helper.h"
#include "example_gui.h"
#include "test.h"
#include "play.h"
#include "game.h"

#if defined(__clang__)
#   pragma clang diagnostic push
#   pragma clang diagnostic ignored "-Wsign-conversion"
#   pragma clang diagnostic ignored "-Wpadded"
#   pragma clang diagnostic ignored "-Wcast-qual"
#   pragma clang diagnostic ignored "-Wcast-align"
#   pragma clang diagnostic ignored "-Wmissing-field-initializers"
#   pragma clang diagnostic ignored "-Wgnu-zero-variadic-macro-arguments"
#   pragma clang diagnostic ignored "-Wmissing-prototypes"
#   pragma clang diagnostic ignored "-Wunused-variable"
#   pragma clang diagnostic ignored "-Wgnu-statement-expression"
#   pragma clang diagnostic ignored "-Wdouble-promotion"
#elif __IS_COMPILER_ARM_COMPILER_5__
#elif __IS_COMPILER_GCC__
#   pragma GCC diagnostic push
#   pragma GCC diagnostic ignored "-Wformat="
#   pragma GCC diagnostic ignored "-Wpedantic"
#endif

/*============================ MACROS ========================================*/

#ifndef __STR
#   define __STR(__A)      #__A
#endif

#ifndef STR
#   define STR(__A)         __STR(__A)
#endif

/*============================ MACROFIED FUNCTIONS ===========================*/
/*============================ TYPES =========================================*/
/*============================ GLOBAL VARIABLES ==============================*/

/*============================ PROTOTYPES ====================================*/
/*============================ LOCAL VARIABLES ===============================*/






/*============================ IMPLEMENTATION ================================*/


static ARM_NOINIT arm_2d_helper_pfb_t s_tExamplePFB;
extern uint8_t Font_16x16_h[];

static volatile bool s_bDrawInfo = true;

extern const arm_2d_tile_t c_tileArrow;

void display_task2(void)
{

   

}

__OVERRIDE_WEAK
void example_gui_on_refresh_evt_handler(const arm_2d_tile_t *ptFrameBuffer)
{
    
}


__OVERRIDE_WEAK
void arm_2d_helper_perf_counter_start(void)
{
    start_cycle_counter();
}

__OVERRIDE_WEAK
int32_t arm_2d_helper_perf_counter_stop(void)
{
    return stop_cycle_counter();
}




extern
const arm_2d_tile_t c_tPictureHeliun;

extern 
const arm_2d_tile_t c_tileBackground;


static
IMPL_PFB_ON_DRAW(__pfb_draw_background_handler)
{
    ARM_2D_UNUSED(pTarget);
    ARM_2D_UNUSED(bIsNewFrame);

    arm_2d_rgb16_fill_colour(ptTile, NULL, GLCD_COLOR_BLACK);
   

    return arm_fsm_rt_cpl;
}


static
IMPL_PFB_ON_LOW_LV_RENDERING(__pfb_render_handler)
{
    const arm_2d_tile_t *ptTile = &(ptPFB->tTile);

    ARM_2D_UNUSED(pTarget);
    ARM_2D_UNUSED(bIsNewFrame);

    //if (0 == BENCHMARK.wIterations || s_bDrawInfo) {
        GLCD_DrawBitmap(ptTile->tRegion.tLocation.iX,
                        ptTile->tRegion.tLocation.iY,
                        ptTile->tRegion.tSize.iWidth,
                        ptTile->tRegion.tSize.iHeight,
                        (uint16_t *)ptTile->pchBuffer);
    //}
    arm_2d_helper_pfb_report_rendering_complete(&s_tExamplePFB,
                                                (arm_2d_pfb_t *)ptPFB);
}




static
IMPL_PFB_ON_DRAW(__pfb_draw_handler)
{
    ARM_2D_UNUSED(pTarget);
    //example_gui_refresh(ptTile, bIsNewFrame);
		//test_paly_game_gui_refresh(ptTile, bIsNewFrame);
		paly_game_gui_refresh(ptTile, bIsNewFrame);
    return arm_fsm_rt_cpl;
}


void play_display_task(void)
{
		IMPL_ARM_2D_REGION_LIST(s_tDirtyRegions2,  static)

        /* a region for the busy wheel */
        ADD_REGION_TO_LIST(s_tDirtyRegions2,
            .tLocation = {
                .iX = 30, //((APP_SCREEN_WIDTH - 222) >> 1),
                .iY = 0,//((APP_SCREEN_HEIGHT - 222) >> 1),
            },
            .tSize = {
                .iWidth = 30,
                .iHeight = 40,
            },
        ),
				
				 
				
				
				 ADD_REGION_TO_LIST(s_tDirtyRegions2,
            .tLocation = {
                .iX = 30, //((APP_SCREEN_WIDTH - 222) >> 1),
                .iY = 0,//((APP_SCREEN_HEIGHT - 222) >> 1),
            },
            .tSize = {
                .iWidth = 30,
                .iHeight = 40,
            },
        ),
        /* a region for the status bar on the bottom of the screen */
        ADD_LAST_REGION_TO_LIST(s_tDirtyRegions2,
            .tLocation = {23+34,30+26+5},
            .tSize = {
                .iWidth = 200-60,
                .iHeight = 200-62,
            },
        ),

    END_IMPL_ARM_2D_REGION_LIST()
		
		uint8_t i_num = 0; 

		for(uint8_t i = 0; i < 2; i++){
				s_tDirtyRegions2[i_num].tRegion.tLocation.iX = play_obstacle[i].iX - tplay_X_Y.iX;
				s_tDirtyRegions2[i_num].tRegion.tLocation.iY = 0;
				if((s_tDirtyRegions2[i_num].tRegion.tLocation.iX < -(31+MOVING_SPEED_iX)) || (s_tDirtyRegions2[i_num].tRegion.tLocation.iX > 240)){
						s_tDirtyRegions2[i_num].tRegion.tSize.iWidth = 0;
						s_tDirtyRegions2[i_num].tRegion.tSize.iHeight= 0;		
				}else{
						s_tDirtyRegions2[i_num].tRegion.tSize.iWidth = 30+MOVING_SPEED_iX;
						s_tDirtyRegions2[i_num].tRegion.tSize.iHeight= 240;	
						i_num++;
				}	
							
				
		}
		
		
		s_tDirtyRegions2[2].tRegion.tLocation.iX = tBird.tLocation.iX;
		s_tDirtyRegions2[2].tRegion.tLocation.iY = tBird.tLocation.iY - tplay_X_Y.iY-3*MOVING_SPEED_iX;
		s_tDirtyRegions2[2].tRegion.tSize.iWidth = tBird.tSize.iWidth;
		s_tDirtyRegions2[2].tRegion.tSize.iHeight= tBird.tSize.iHeight+7*MOVING_SPEED_iX;
		
		while(arm_fsm_rt_cpl != arm_2d_helper_pfb_task( 
					 &s_tExamplePFB, NULL));			
         //&s_tExamplePFB, (arm_2d_region_list_item_t *)s_tDirtyRegions2));
		
		
		
		//play_game();//==================================================================
		play_game_ys();
}

void example_gui_init222(void);
void busy_wheel_init(void);
/*----------------------------------------------------------------------------
  Main function
 *----------------------------------------------------------------------------*/
int main (void)
{
#if defined(__IS_COMPILER_GCC__)
    app_platform_init();
#endif

    arm_irq_safe {
        arm_2d_init();
        /* put your code here */
        //example_gui_init();
				//progress_bar_drill_init();
				//example_gui_init222();
    }

    //! initialise FPB helper
    if (ARM_2D_HELPER_PFB_INIT(
            &s_tExamplePFB,                 //!< FPB Helper object
            APP_SCREEN_WIDTH,               //!< screen width
            APP_SCREEN_HEIGHT,              //!< screen height
            uint16_t,                       //!< colour date type
            PFB_BLOCK_WIDTH,                //!< PFB block width
            PFB_BLOCK_HEIGHT,               //!< PFB block height
            1,                              //!< number of PFB in the PFB pool
            {
                .evtOnLowLevelRendering = {
                    //! callback for low level rendering
                    .fnHandler = &__pfb_render_handler,
                },
                .evtOnDrawing = {
                    //! callback for drawing GUI
                    .fnHandler = &__pfb_draw_background_handler,
                },
            },
            //.FrameBuffer.bSwapRGB16 = true,
        ) < 0) {
        //! error detected
        assert(false);
    }

    //! draw background first
    while(arm_fsm_rt_cpl != arm_2d_helper_pfb_task(&s_tExamplePFB,NULL));

    s_bDrawInfo = false;

    ARM_2D_HELPER_PFB_UPDATE_ON_DRAW_HANDLER(   &s_tExamplePFB,
                                                &__pfb_draw_handler);
		key_init();
    while (1) {
        //display_task();
				play_display_task();
    }
}

#if defined(__clang__)
#   pragma clang diagnostic pop
#endif

